<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
// 红灯3秒亮一次，黄灯2秒亮一次，绿灯1秒亮一次；如何让三个灯不断交替重复亮灯？（用Promise实现）三个亮灯函数已经存在：
  function red() {
    console.log("red");
  }
  function green() {
    console.log("green");
  }
  function yellow() {
    console.log("yellow");
  }
  
  function light(time,color){
    return new Promise(resolve=>{
      setTimeout(() => {
        color()
        resolve()
      }, time);
    })
  }

  function step(){
    // light(3000,red).then(res=>{
    //   light(2000,yellow).then(res=>{
    //     light(1000,green).then(res=>{
    //       step()
    //     })
    //   })
    // })
    Promise.resolve().then(()=>{
      return light(3000,red)
    }).then(()=>{
      return light(2000,yellow)
    }).then(()=>{
      return light(1000,green)
    }).then(()=>{
      step()
    })
  }
  step()
  </script>
</body>
</html>